package com.ruoyi.project.api.controller;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.api.domain.OrderPay;
import com.ruoyi.project.price.service.IBinsAccountTraceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @title : PayController
 * @Author : Administrator
 * @Date : 2025/4/9
 * @Version : 1.0.0
 * @Description :
 */
@RestController
@RequestMapping("/api/pay")
public class PayController {

    private Logger logger = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private IBinsAccountTraceService accountTraceService;

    /**
     * 用户充值
     * @param pay
     * @return
     */
    @RequestMapping("/create")
    public AjaxResult createPay(@RequestBody OrderPay pay) {
        logger.info("createPay:{}", pay);
        Map map = new HashMap<>();
        map.put("orderInfo","orderInfo");
        accountTraceService.getAccountTrace2Pay(pay.getUserId(), pay.getAmount(), "2");
        return AjaxResult.success(map);
    }

    /**
     * 用户退款，体现
     * @param pay
     * @return
     */
    @RequestMapping("/refund")
    public AjaxResult refundPay(@RequestBody OrderPay pay) {
        logger.info("createPay:{}", pay);
        Map map = new HashMap<>();
        map.put("orderInfo","orderInfo");
        BigDecimal refundAmount = pay.getAmount();
        if (pay.getAmount()!=null){
            if (refundAmount.compareTo(BigDecimal.ZERO)==0){
                return AjaxResult.error("退款金额不能等于0");
            }
            if (refundAmount.compareTo(BigDecimal.ZERO)>0){
                refundAmount = refundAmount.negate();
            }
        }
        accountTraceService.getAccountTrace2Pay(pay.getUserId(), refundAmount, "1");
        return AjaxResult.success();
    }

}
